package com.sky.controller.user;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrdersPageQueryVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/user/order")
public class OrderUserController {
    @Autowired
    OrderService orderService;
    /**
     * 提交订单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrderSubmitVO submit = orderService.submit(ordersSubmitDTO);
        return Result.success(submit);

    }
    /**
     * 订单支付
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result pay(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        System.out.println("要支付的订单号是:"+ordersPaymentDTO.getOrderNumber());
        //todo 这里要和微信交互
        return Result.success(OrderPaymentVO.builder().build());
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result moreOrder(@PathVariable Long id){
        orderService.moreOrder(id);
        return Result.success();
    }

    /**
     * 用户端取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable("id") Long id) throws Exception {
        orderService.cancel(id);
        return Result.success();
    }
    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation("历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        PageResult result = orderService.historyOrders(ordersPageQueryDTO);
        return Result.success(result);
    }
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result orderDetail(@PathVariable("id") Long id){
        OrdersPageQueryVO orderPageQueryVO = orderService.findOrderDetail(id);

        return Result.success(orderPageQueryVO);
    }
    @ApiOperation("催单")
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
       orderService.reminder(id);
       return Result.success();
    }
}
